
































































































































package com.ourhours.message.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ourhours.message.domain.SmsSend;
import com.ourhours.message.enums.ApiResponseCode;
import com.ourhours.message.exception.MessageException;
import com.ourhours.message.mapper.SmsSendMapper;
import com.ourhours.message.service.SmsSendService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ourhours.message.service.TemplateService;
import com.ourhours.message.service.wrapper.SmsWrapper;
import com.ourhours.message.service.wrapper.SupplierWrapper;
import com.ourhours.message.utils.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 消息发送记录表 服务实现类
 * </p>
 *
 * @author dalong.wang
 * @since 2018-03-21
 */
@Service
public class SmsSendServiceImpl extends ServiceImpl<SmsSendMapper, SmsSend> implements SmsSendService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmsSendServiceImpl.class);

    @Autowired
    SmsWrapper messageWrapper;

    @Autowired
    SmsSendService smsSendService;

    @Autowired
    SupplierWrapper supplierWrapper;

    @Override
    public Boolean sendMessage(SmsSend smsSend) {
        String phone = smsSend.getPhone();
        Long vendorId = smsSend.getVendorId();
        String content = smsSend.getContent();
        try{
            Integer sendNumber = this.verifyNumber(vendorId,phone);
            if(sendNumber>10){
                LOGGER.error("phoneSmsRpcService.sendMessage 发送短信每天超过限制：参数:{}{}{}, 结果:{}",phone,vendorId,content,sendNumber);
                return false;
            }
            boolean result = messageWrapper.send(phone,vendorId,content);
            LOGGER.info("phoneSmsRpcService.sendMessage 发送短信成功：参数:{}，{}，{}, 结果:{}",phone,vendorId,content,result);
            return result;
        } catch (Exception e) {
            LOGGER.error("phoneSmsRpcService.sendMessage 发送短信失败：参数:{}，{}，{}, 结果:{}",phone,vendorId,content, e);
            return false;
        }
    }
    /**
     * 验证数量
     * @return
     */
    public Integer verifyNumber(Long vendorId,String phone){
        SmsSend smsSend = new SmsSend();
        smsSend.setVendorId(vendorId);
        smsSend.setPhone(phone);
        Date startDate = DateFormatUtils.formatToday("yyyy-MM-dd 00:00:00");
        Date endDate = DateFormatUtils.formatToday("yyyy-MM-dd 23:59:59");
        EntityWrapper<SmsSend> entityWrapper = new EntityWrapper<SmsSend>(smsSend);
        entityWrapper.between("create_time", startDate, endDate);
        List<SmsSend> smsSendList = smsSendService.selectList(entityWrapper);
        return smsSendList.size();
    }



    public Boolean verifyPhoneVacode(SmsSend smsSend) {
        Date current = new Date();
        SmsSend criteria = new SmsSend();
        criteria.setPhone(smsSend.getPhone());
        criteria.setVendorId(smsSend.getVendorId());
        criteria.setCode(smsSend.getCode());
        criteria.setSmsType(smsSend.getSmsType());
        EntityWrapper<SmsSend> entityWrapper = new EntityWrapper<SmsSend>(criteria);
        SmsSend smsSendEntity = smsSendService.selectOne(entityWrapper);
        if(smsSendEntity==null){
            LOGGER.error("SmsSendServiceImpl.verifyPhoneVacode 获取验证码失败：参数:{}, 结果:{}", smsSend,false);
            throw new MessageException(ApiResponseCode.CODE_ERROR,ApiResponseCode.CODE_ERROR.getMessage());
        }
        Date createTime = smsSendEntity.getCreateTime();
        long result =(current.getTime() - createTime.getTime())/1000;

        if(result>300){//不在有效期内
            LOGGER.error("SmsSendServiceImpl.verifyPhoneVacode 短信验证码不在有效期：参数:{}, 结果:{}", smsSendEntity,false);
            throw new MessageException(ApiResponseCode.CODE_TIME_ERROR,ApiResponseCode.CODE_TIME_ERROR.getMessage());
        }

        smsSendEntity.setStatus(3);
        smsSendService.updateById(smsSendEntity);
        LOGGER.info("SmsSendServiceImpl.verifyPhoneVacode 检验手机验证成功：参数:{}, 结果:{}", JSON.toJSON(smsSendEntity),true);
        return true;
    }



    public void setSmsSendStatus(Long id, Integer status, String errCode, String reason) {
        try {
            SmsSend entity = new SmsSend();

            entity.setId(id);
            entity.setStatus(status);
            if (status == -1) {
                entity.setFailureReason(errCode);
                entity.setFailureReason(reason);
            }
            entity.setUpdateTime(new Date());
            entity.setUpdateUser("MQ");
            this.updateById(entity);
        } catch (Exception e) {
            LOGGER.error("SmsSendService.setSmsSendStatus 保存记录状态失败，但不处理异常，参数:{},{},{},{},原因:{}", id, status, errCode, reason, e);
        }
    }

}














